package com.guodexian.xiaomi.service.impl;

import com.guodexian.xiaomi.dao.CategoryDao;
import com.guodexian.xiaomi.dao.impl.Sql2oCategoryDaoImpl;
import com.guodexian.xiaomi.entity.Category;
import com.guodexian.xiaomi.service.CategoryService;
import com.guodexian.xiaomi.tx.TransactionManager;
import com.guodexian.xiaomi.vo.ResultVO;

import java.util.List;
import java.util.Map;

/**
 * 类说明：  <br>
 *
 * @author 南阳德刚<br>
 * 2024-12-30 17:23<br>
 * @since 1.0 <br>
 * 培养IT高薪技术人才的摇篮---北京学佳澳。<br>
 * 点击<a href="http://www.xuejiaao.com/images/qrcode.jpg">学佳澳教育</a>关注公共号,有惊喜(⊙o⊙)<br>
 */
public class CategoryServiceImpl implements CategoryService {
    private CategoryDao categoryDao=new Sql2oCategoryDaoImpl();
    @Override
    public List<Category> getCategoryList(int page, int limit, Map<String, Object> searchMap) {
        return categoryDao.find(page,limit,searchMap);
    }
    @Override
    public long getCategoryCount(Map<String, Object> searchMap) {
        return categoryDao.getCount(searchMap);
    }

    @Override
    public ResultVO saveCategory(Category category) {
        try {
            TransactionManager.beginTransaction();
            Category dbCate = categoryDao.findByName(category.getName());
            ResultVO resultVO;
            if (dbCate != null) {
                resultVO = ResultVO.fail("名字已经存在");
            }else {
                int ret = categoryDao.save(category);
                resultVO = ResultVO.success(ret);
            }
            TransactionManager.commitTransaction();
            return resultVO;
        } catch (Exception e) {
            e.printStackTrace();
            TransactionManager.rollbackTransaction();
            return  ResultVO.fail(e.getMessage());
        }
    }

    @Override
    public ResultVO updateCategory(Category category) {
        try {
            TransactionManager.beginTransaction();
            Category dbCate = categoryDao.findByName(category.getName());
            if (dbCate != null) {
                return ResultVO.fail("名字已经存在");
            }
            int ret = categoryDao.update(category);
            TransactionManager.commitTransaction();
            if (ret > 0) {
                return ResultVO.success(ret);
            }else {
                return  ResultVO.fail("更新失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionManager.rollbackTransaction();
            return  ResultVO.fail(e.getMessage());
        }
    }

    @Override
    public int deleteCategory(Long categoryId) {
        try {
            TransactionManager.beginTransaction();
            int ret = categoryDao.delete(categoryId);
            return ret;
        } catch (Exception e) {
            e.printStackTrace();
            TransactionManager.rollbackTransaction();
            return  0;
        }
    }
    @Override
    public int deleteCategoryByIds(Long[] categoryIds) {
        try {
            TransactionManager.beginTransaction();
            int ret = categoryDao.deleteBatch(categoryIds);
            TransactionManager.commitTransaction();
            return ret;
        } catch (Exception e) {
            e.printStackTrace();
            TransactionManager.rollbackTransaction();
            return 0;
        }
    }
}